<script setup lang="ts">
import { navigateTo, getCurrentInstance, useDidShow, showToast, showLoading } from '@tarojs/taro';
import { state } from './state';
import { getSysImgUrl } from '/@/utils/index';
import { ASSET_BY_APP_DETAIL_QUERY } from '/@/api/modules/equipment/asset-manage';
import { Right as AppNutIconRight } from '@nutui/icons-vue-taro';
import { ASSET_STATUS } from '/@/common/constant';
import { QUERY_CHANGE_DETAIL_BY_ASSET_ID } from '/@/api/modules/equipment/asset-change';
const params = getCurrentInstance().router?.params as {
  assetId: string;
};
/**
 * 查询详情页面
 */
function queryDetail() {
  ASSET_BY_APP_DETAIL_QUERY({ ids: [params.assetId] })
    .then((res) => {
      state.assetInfo = res.assetInfo;
      if (!res.content || !res.content.length) {
        return;
      }
      state.value1 = res.content[0].titleNum || 0;
      state.value2 = res.content[1].titleNum || 0;
      state.value3 = res.content[2].titleNum || 0;
    })
    .catch((e) => console.log(e));
}
useDidShow(() => {
  queryDetail();
});

/**
 * 设备变动详情
 */
function queryChangeDetail() {
  showLoading();
  QUERY_CHANGE_DETAIL_BY_ASSET_ID({ assetId: params.assetId, changeType: 1, changeStatus: 3 }).then(
    (res) => {
      if (res.changeDtoList && res.changeDtoList.length > 0) {
        navigateTo({
          url: `/pages/asset-change/borrow-detail/index?id=${res.changeDtoList[0].id}`,
        });
      } else {
        showToast({ title: '该设备没有领用,无须归还', icon: 'error' });
      }
    },
  );
}
</script>

<template>
  <view class="scan-success app-page--1">
    <app-navbar title="扫描结果" />
    <scroll-view class="app-page__wrapper" :scroll-y="true" :lower-threshold="100">
      <view class="scan-success__wrapper">
        <view class="scan-success__title">
          <text>{{ state.assetInfo?.assetName || '_' }}</text>
        </view>
        <view class="scan-success__img-container" v-if="state.assetInfo?.picUrl">
          <image
            class="scan-success__img-container__img"
            :src="getSysImgUrl(state.assetInfo?.picUrl)"
          />
        </view>
        <nut-cell-group>
          <nut-cell title="资产编号" :desc="state.assetInfo?.assetCode || '_'" />
          <nut-cell title="资产分类" :desc="state.assetInfo?.assetTypeName || '_'" />
          <nut-cell title="品牌" :desc="state.assetInfo?.brandName || '_'" />
          <nut-cell title="型号" :desc="state.assetInfo?.modelName || '_'" />
          <nut-cell
            title="资产状态"
            :desc="state.assetInfo?.status ? ASSET_STATUS[state.assetInfo?.status] : '_'"
          />
          <nut-cell title="所属区域" :desc="state.assetInfo?.areaName || '_'" />
          <nut-cell title="最近维修" :desc="'_'" />
          <nut-cell title="最近保养" :desc="'_'" />
        </nut-cell-group>
        <view
          class="scan-success__more"
          @click="
            navigateTo({
              url: `/pages/device-account/detail/index?id=${params.assetId}&deviceId=${state.assetInfo?.deviceId}`,
            })
          "
        >
          <text>更多</text><app-nut-icon-right size="16" color="#507AEE" />
        </view>
        <view class="scan-success__footer" v-if="state.appEdition != 'can-nang-third-hospital'">
          <nut-button
            class="scan-success__footer-btn"
            shape="round"
            type="info"
            @click="
              navigateTo({
                url: `/pages/work-order/edit/index?assetId=${params.assetId}&from=scan`,
              })
            "
            ><text>故障上报</text></nut-button
          >
          <nut-button
            class="scan-success__footer-btn"
            shape="round"
            type="info"
            @click="
              navigateTo({
                url: `/pages/scan/work-order/list/index?assetId=${params.assetId}`,
              })
            "
          >
            <text> 维修工单 </text>
          </nut-button>
          <nut-badge
            :value="state.value1"
            color="#ff3e3e"
            :max="99"
            class="scan-success__nut-badge"
          />
        </view>
        <view class="scan-success__footer">
          <nut-button
            class="scan-success__footer-btn"
            shape="round"
            type="info"
            @click="
              navigateTo({
                url: `/pages/scan/inspection/list/index?assetId=${params.assetId}`,
              })
            "
            ><text>巡检任务</text>
          </nut-button>
          <nut-badge
            :value="state.value2"
            color="#ff3e3e"
            :max="99"
            class="scan-success__nut-badge"
          />
          <nut-button
            class="scan-success__footer-btn"
            shape="round"
            type="info"
            @click="
              navigateTo({
                url: `/pages/scan/maintenance/list/index?assetId=${params.assetId}`,
              })
            "
          >
            <text>保养任务</text>
          </nut-button>
          <nut-badge
            :value="state.value3"
            color="#ff3e3e"
            :max="99"
            class="scan-success__nut-badge"
          />
        </view>
      </view>
    </scroll-view>
    <view class="scan-success__footer" v-if="state.appEdition === 'can-nang-third-hospital'">
      <nut-button
        class="scan-success__footer-btn"
        shape="round"
        type="info"
        @click="
          navigateTo({
            url: `/pages/asset-change/receive-edit/index?assetId=${params.assetId}`,
          })
        "
        >领用</nut-button
      >
      <nut-button
        class="scan-success__footer-btn"
        shape="round"
        type="info"
        @click="queryChangeDetail"
        >归还</nut-button
      >
      <nut-button
        class="scan-success__footer-btn"
        shape="round"
        type="info"
        @click="
          navigateTo({
            url: `/pages/asset-change/handle-edit/index?assetId=${params.assetId}`,
          })
        "
        >出库</nut-button
      >
      <nut-button
        class="scan-success__footer-btn"
        shape="round"
        type="info"
        @click="
          navigateTo({
            url: `/pages/asset-change/allocation-edit/index?assetId=${params.assetId}`,
          })
        "
        >调拨</nut-button
      >
    </view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
